package com.probiz.estore.system.service.dwr;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.common.model.system.Region;
import com.probiz.estore.common.model.system.RegionVO;
import com.probiz.estore.system.service.RegionManager;

public class RegionDwr {
	private static transient Log log = LogFactory.getLog(RegionDwr.class);
	private RegionManager regionManager;
	
	public void setRegionManager(RegionManager regionManager){
		this.regionManager=regionManager;
	}

	public RegionVO getRegionById(Integer id) {
		Region region=this.regionManager.getById(id);
		RegionVO result = new RegionVO(region);		
		return result;
	}
	/**
	 * 根据父地区id获得所有子地区
	 * @param parentId
	 * @return
	 */
	public List getRegionsByParentId(Integer parentId) {
		Region parentRegion = regionManager.getById(parentId);
		return convertToRegionVos(parentRegion.getChildren());
		
		
	}
	
	/**
	 * 转化为VO对象
	 * @param regionList
	 * @return
	 */
	private List<RegionVO> convertToRegionVos(Set<Region> regionSet){
		
		 List<RegionVO> regionVOList=new ArrayList<RegionVO>();
		 for(Region region:regionSet){
			 regionVOList.add(new RegionVO(region) );
		 }
		 return regionVOList;
	}
	/**
	 * 根据地区id获得地区全名
	 * @param regionId
	 * @return
	 */
	public String getRegionFullNameByRegionId(Integer regionId){
		Region region=this.regionManager.getById(regionId);
		return region.getFullName();
	}
}
